﻿using Dapper;
using Infrastructure.DataBaseConnect;
using Infrastructure.IRepository.Sys;
using Model;
using Model.BusinessModel;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Infrastructure.Repository.Sys
{
    /// <summary>
    /// 角色仓储接口实现
    /// </summary>
    public class SysRoleRepository : Repository<SysRole>, ISysRoleRepository
    {
        /// <summary>
        /// 获取全部角色
        /// </summary>
        /// <returns></returns>
        public List<SysRoleOutPut> GetAllRoleTreeList(string? corporationKey)
        {
            StringBuilder sql = new(@" SELECT sy.*,sc.CorporationName,ss.SystemName,su.UserName FROM Sys_Role as sy
                            inner join Sys_Corporation as sc on sy.CorporationKey = sc.CorporationKey
                            inner join Sys_System as ss on ss.SystemKey = sy.SystemKey 
                            left join Sys_User as su on sy.CreateUser = su.UserId");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey) && Const.XC_CorporationKey != corporationKey)
            {
                qp.listWhere.Append(" and sc.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            sql.Append("  order by sy.CreateTime desc");

            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysRoleOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

        /// <summary>
        /// 根据公司和系统获取数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<SysRole> GetRoleByCorporationKeyOrSystemKey(string corporationKey, string systemKey)
        {
            string sql = @" select * from Sys_Role where CorporationKey=@CorporationKey and SystemKey=@SystemKey ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<SysRole>(sql, new { CorporationKey = corporationKey, SystemKey = systemKey }).ToList();
        }

        /// <summary>
        /// 根据角色名称获取数据
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <param name="roleName">角色名称</param>
        /// <returns></returns>
        public SysRole? GetRoleByName(string corporationKey, string systemKey, string roleName)
        {
            string sql = @" select * from Sys_Role where CorporationKey=@CorporationKey and SystemKey=@SystemKey and RoleName=@RoleName ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.QueryFirstOrDefault<SysRole>(sql, new { CorporationKey = corporationKey, SystemKey = systemKey, RoleName = roleName });
        }

        /// <summary>
        /// 设置系统是否启用
        /// </summary>
        /// <param name="keys">key集合</param>
        /// <param name="isOpen">是否启用</param>
        public void SetRoleIsOpen(List<string> keys, bool isOpen)
        {
            string sql = "   update Sys_Role set IsOpen=@IsOpen where RoleId in @RoleId  ";
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            connection.Query(sql, new { RoleId = keys, IsOpen = isOpen });
        }

        /// <summary>
        /// 获取角色
        /// </summary>
        /// <param name="corporationKey">公司key</param>
        /// <param name="systemKey">系统key</param>
        /// <returns></returns>
        public List<TreeOutPut> GetRoleList(string corporationKey, string systemKey)
        {
            StringBuilder sql = new(@"   
                                 select * from (select  CONVERT(VARCHAR(36), RoleId) as [Key],case WHEN Pid='0' THEN  CONVERT(VARCHAR(36), SystemKey) else CONVERT(VARCHAR(36), Pid) end AS [PKey]  ,RoleName as  label,CorporationKey,SystemKey,(case RoleType when 1 then 1 else  0 end ) as disabled 
                                 from  Sys_Role 
                                 union all 
                                  select   SystemKey as [Key],CorporationKey AS [PKey]  ,SystemName as  label,CorporationKey,SystemKey,1 as disabled 
                                 from  Sys_System ) t ");

            var qp = new QueryParameter();
            if (!string.IsNullOrWhiteSpace(corporationKey))
            {
                qp.listWhere.Append(" and t.CorporationKey = @CorporationKey ");
                qp.dynamicParameter.Add("@CorporationKey", corporationKey);
            }
            if (!string.IsNullOrWhiteSpace(systemKey))
            {
                qp.listWhere.Append(" and t.SystemKey = @SystemKey ");
                qp.dynamicParameter.Add("@SystemKey", systemKey);
            }
            if (!string.IsNullOrEmpty(qp.listWhere.ToString()))
            {
                sql.Append(" WHERE 1=1 " + qp.listWhere.ToString());
            }
            using var connection = DataBaseConnectConfig.GetSqlConnection();
            return connection.Query<TreeOutPut>(sql.ToString(), qp.dynamicParameter).ToList();
        }

    }
}
